import pygame

from Config import WIDTH, MAXMAP,MAXLV
from My_functions import create_health_bar,render_text,create_exp_bar

BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
YELLOW = (252,203,22)
H_YELLOW= (251,218,65)

pygame.font.init()
font = pygame.font.Font(None, 24)
sfont = pygame.font.Font(None, 16)

class StatusBar(pygame.sprite.Sprite):
    def __init__(self, character,width=None ,height=10, x_offset=0, y_offset=-8):
        super().__init__()
        self.character = character  # 角色对象
        self.x_offset = x_offset  # x 方向偏移
        self.y_offset = y_offset
        if width is None:width = character.rect.width
        self.width=width
        self.height=height
        self.image = pygame.Surface((width, height), pygame.SRCALPHA)  # 创建透明图像
        self.rect = self.image.get_rect()
        self.live = character.live

    def update(self):
        """更新血条状态"""
        self.image=create_health_bar(self.character,self.width,self.height)

        # 更新血条的位置，始终跟随角色
        self.rect.center = (self.character.rect.centerx + self.x_offset, self.character.rect.top + self.y_offset)

        self.live = self.character.live

class Player1StatusBar(StatusBar):
    def update(self):
        #清屏
        self.image.fill((0,0,0,0))

        #角色血量条
        self.image.blit(create_health_bar(self.character, self.width,20,'l'),(0,20))
        player_hp = render_text(f"{self.character.hp}/{self.character.max_hp}", 16, BLACK)
        self.image.blit(player_hp, (self.width//2-player_hp.get_width()//2, 20))

        #角色经验条
        self.image.blit(create_exp_bar(self.character, self.width, 20, 'l'), (0, 45))
        if self.character.level == MAXLV:player_exp = render_text("Max",16,BLACK)
        else :player_exp = render_text(f"{self.character.exp}/{self.character.max_exp}", 16, BLACK)
        self.image.blit(player_exp, (self.width // 2 - player_hp.get_width() // 2, 45))

        #角色名称和等级
        player_name_and_lv = render_text(f"{self.character.name} Lv.{self.character.level}", 16, BLACK)
        self.image.blit(player_name_and_lv, (0, 0))

        self.rect.x=5
        self.rect.y=5

        self.live=self.character.live

class Player2StatusBar(StatusBar):
    def update(self):

        #清屏
        self.image.fill((0, 0, 0, 0))

        #角色血量条
        self.image.blit(create_health_bar(self.character, self.width,  20, 'r'), (0, 20))
        player_hp = render_text(f"{self.character.hp}/{self.character.max_hp}", 16, BLACK)
        self.image.blit(player_hp, (self.width // 2 - player_hp.get_width() // 2, 20))

        # 角色经验条
        self.image.blit(create_exp_bar(self.character, self.width, 20, 'r'), (0, 45))
        if self.character.level == MAXLV:
            player_exp = render_text("Max", 16, BLACK)
        else:
            player_exp = render_text(f"{self.character.exp}/{self.character.max_exp}", 16, BLACK)
        self.image.blit(player_exp, (self.width // 2 - player_hp.get_width() // 2, 45))

        #角色名称和等级
        player_name_and_lv = render_text(f"{self.character.name} Lv.{self.character.level}", 16, BLACK)
        self.image.blit(player_name_and_lv, (self.width - player_name_and_lv.get_width(), 0))

        self.rect.x = WIDTH-self.width-5
        self.rect.y = 5

        self.live = self.character.live

